package br.usp.ime.ingpos.testes.services;

import java.util.ArrayList;

import org.mockito.Mockito;

import br.usp.ime.ingpos.modelo.Interesse;
import br.usp.ime.ingpos.modelo.dao.InteresseDAO;
import br.usp.ime.ingpos.services.InteresseService;
import br.usp.ime.ingpos.testes.BancoDeDadosTestCase;

public class InteresseServiceTeste extends BancoDeDadosTestCase {
	
	private InteresseDAO interesseDAO;
	private InteresseService interesseService;
	
	public InteresseServiceTeste(String name) {
		super(name);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		
		interesseDAO = Mockito.mock(InteresseDAO.class);
		interesseService = new InteresseService(interesseDAO);
	}
	
	public void testSalvaInteresse() {
		Long idProfessor = 42L;
		
		ArrayList<Long> idsInscricoes = new ArrayList<Long>();
		ArrayList<Long> idsInteressantes = new ArrayList<Long>();
		
		idsInscricoes.add(1L);
		idsInscricoes.add(2L);
		idsInscricoes.add(3L);
		idsInscricoes.add(4L);
		
		idsInteressantes.add(3L);
		
		Interesse interesse1 = Mockito.mock(Interesse.class);
		Mockito.when(interesse1.isSugeridoPeloCoordenador()).thenReturn(false);
		Interesse interesse2 = Mockito.mock(Interesse.class);
		Mockito.when(interesse2.isSugeridoPeloCoordenador()).thenReturn(true);
		
		
		Mockito.when(interesseDAO.procuraInteresse(42L, 1L)).thenReturn(interesse1);
		Mockito.when(interesseDAO.procuraInteresse(42L, 2L)).thenReturn(null);
		Mockito.when(interesseDAO.procuraInteresse(42L, 3L)).thenReturn(null);
		Mockito.when(interesseDAO.procuraInteresse(42L, 4L)).thenReturn(interesse2);
		
		interesseService.salvaInteresse(idProfessor, idsInteressantes, idsInscricoes);
		
		Mockito.verify(interesseDAO).delete(interesse1);
		
		Mockito.verify(interesse2).setInteressePeloOrientador( false );		
		Mockito.verify(interesseDAO).saveOrUpdate(interesse2);
	}
	
	public void testeGetInscricoesComInteressePorOrientadorID() {
		Long idProfessor = 42L;
		
		ArrayList<Interesse> listaInteresse = new ArrayList<Interesse>();
		Interesse interesse1 = Mockito.mock(Interesse.class);
		Mockito.when(interesse1.getIdInscricao()).thenReturn(1L);
		Interesse interesse2 = Mockito.mock(Interesse.class);
		Mockito.when(interesse2.getIdInscricao()).thenReturn(2L);
		Interesse interesse3 = Mockito.mock(Interesse.class);
		Mockito.when(interesse3.getIdInscricao()).thenReturn(3L);
		
		listaInteresse.add(interesse1);
		listaInteresse.add(interesse2);
		listaInteresse.add(interesse3);
		
		Mockito.when(interesseDAO.filtraPorOrientadorID(idProfessor)).thenReturn(listaInteresse);
		
		ArrayList<Long> saida = new ArrayList<Long>();
		saida.add(1L);
		saida.add(2L);
		saida.add(3L);
		
		assertEquals(interesseService.getInscricoesComInteressePorOrientadorID(idProfessor), saida);
	}
}
